<?php


namespace app\admin\controller;

use app\common\model\User;
use Cache;

class Center extends Common
{
    public function index()
    {
        if (input('get.keywords')) {
            $us_id = model("User")
                ->where('us_nickname|us_tel', 'like', '%' . input('get.keywords') . '%')
                ->value('id');
            if (!$us_id) {
                $us_id = 0;
            }
            $this->map[] = ['id', '=', $us_id];
        }
        if (input('get.yunId')) {
            $this->map[] = ['id', '=', input('get.yunId')];
        }
        $list = User::where($this->map)
            ->where('us_type', 3)->select();

        if (count($list) != 0) {

            // 运营中心下级团队,自身 id 为key 值
            foreach ($list as $k => $v) {
                $yunId[$v['id']] = $this->getAllChildrenId($v['id']);
            }
            foreach ($yunId as $k => $v) {
                array_unshift($yunId[$k], $k);
            }
            foreach ($yunId as $k => $v) {
                $yuanYunId = model('User')->whereIn('yun_yuan_pid', $v)->field('id,yun_yuan_yeji')->select();
//            $yuanMoney = model('User')->whereIn('yun_yuan_pid',$v)->sum('yun_yuan_yeji');
//            var_dump($yuanMoney);
                $yunId[$k] = model('user')->where('id', $k)->field('us_tel,us_nickname,id')->find();
                $yunId[$k]['teamMoney'] = model('MoneyDetailed')
                    ->where('type', '=', 1)
                    ->whereIn('us_id', $v)
                    ->sum('money');

                if (!empty($yuanYunId)) {
                    foreach ($yuanYunId as $key => $value) {
                        $yunId[$k]['teamMoney'] = $value['yun_yuan_yeji'] + $yunId[$k]['teamMoney'];
                    }
                }

                //$yunId[$k]['allTeamMoney'] = 77777777777777777+$yunId[$k]['teamMoney'];
                $yunId[$k]['award'] = \db('config')->where('id', 154)->value('value');

            }
        }else {
            $yunId = $list;
        }
        $this->assign(['list' => $yunId]);
        return $this->fetch();


//dump($yunId);

//        if (empty($list)) {
//            $this->assign('list', $list);
//            return $this->fetch();
//        }
        // 循环查询团队
//        foreach ($list as $id) {
//           $all[$id] = $this->getAllChildrenId($id);
//        }
//        var_dump($all);
//        // 把运营中心自身id 加入到数组中
//        foreach ($all as $k=>$v){
//            array_unshift ($all[$k], $k);
//        }
//        halt($yunId);

//        $aaaa = $this->array_to_object($yunId);
//        halt($aaaa);
//
//        var_dump($yunId);
//        exit();


    }

    public function team()
    {

        $idy = input('get.id');
        $teams = $this->getAllChildrenId($idy);
        array_unshift($teams, $idy);

        $data = model('user')->whereIn('id', $teams)
            ->field('id,us_nickname,us_tel,us_type')
            ->select();

        $yuanYunInfo = model('User')->whereIn('yun_yuan_pid', $teams)
            ->field('id,yun_yuan_pid,yun_yuan_yeji')
            ->select();
        foreach ($data as $k => $v) {
            $data[$k]['teamMoney'] = model('MoneyDetailed')
                ->where('type', '=', 1)
                ->where('us_id', $v['id'])
                ->sum('money');
        }
        $money = model('MoneyDetailed')
            ->where('type', '=', 1)
            ->where($this->map)
            ->whereIn('us_id', $teams)
            ->sum('money');

        //  统计原店主升级运营中心前时的业绩
        if (!empty($yuanYunInfo)) {
            $a = 0;
            foreach ($yuanYunInfo as $key => $value) {
                $team['yuan_yuan_id'] = $value['yun_yuan_pid'];
                $team['yuan_yuan_money'] = $value['yun_yuan_yeji'];
                $a += $team['yuan_yuan_money'];
            }
        }

        //  原运营中心信息
        //  原运营中心业绩汇总
        //  money: 现有团队业绩和
        //  list : 每个用户所属信息

//      dump($yuanYunInfo);  dump($a);dump($data);halt($money);
        $this->assign([
            'idy' => $idy,
            'list' => $data,
            'money' => $money,
            'a' => $a,
            'yuan_yun_info' => $yuanYunInfo
        ]);
        return $this->fetch();


    }

    public function teams()
    {

        $idy = input('get.id');
        $teams = $this->getAllChildrenId($idy);
        array_unshift($teams, $idy);

        $data = model('user')->whereIn('id', $teams)
            ->field('id,us_nickname,us_tel,us_type')
            ->select();

        $yuanYunInfo = model('User')->whereIn('yun_yuan_pid', $teams)
            ->field('id,yun_yuan_pid,yun_yuan_yeji,us_nickname,us_tel')
            ->select();
        foreach ($data as $k => $v) {
            $data[$k]['teamMoney'] = model('MoneyDetailed')
                ->where('type', '=', 1)
                ->where('us_id', $v['id'])
                ->sum('money');
        }
        $money = model('MoneyDetailed')
            ->where('type', '=', 1)
            ->where($this->map)
            ->whereIn('us_id', $teams)
            ->sum('money');

        //  统计原店主升级运营中心前时的业绩
        if (!empty($yuanYunInfo)) {
            $a = 0;
            foreach ($yuanYunInfo as $key => $value) {
                $team['yuan_yuan_id'] = $value['yun_yuan_pid'];
                $team['yuan_yuan_money'] = $value['yun_yuan_yeji'];
                $a += $team['yuan_yuan_money'];
            }
        }

        //  原运营中心信息
        //  原运营中心业绩汇总
        //  money: 现有团队业绩和
        //  list : 每个用户所属信息

//        halt($yuanYunInfo);

//      dump($yuanYunInfo);  dump($a);dump($data);halt($money);
        $this->assign([
            'idy' => $idy,
            'list' => $data,
            'money' => $money,
            'a' => $a,
            'yuan_yun_info' => $yuanYunInfo
        ]);
        return $this->fetch();


    }

    // 供货商通过提现
    public function gchange()
    {
        if (is_post()) {
            $data[input('post.key')] = input('post.value');
            $data['tx_adopt_time'] = date('Y-m-d H:i:s');
            $rel = model('MoneyDetailed')->save($data, ['id' => input('post.id')]);
            if ($rel) {
                return ['code' => '1', 'msg' => '审批成功'];
            } else {
                return ['code' => '0', 'msg' => '审批失败'];
            }
        }
    }

    // 供货商驳回提现
    public function gchange2()
    {
        if (is_post()) {
            $tx_id = input('post.id');  //  提现id
            $us_id = model('MoneyDetailed')->where('id', $tx_id)->value('us_id');

            $data['y_pay_money'] = model('user')->where('id', $us_id)->value('us_all_get');
            $data[input('post.key')] = input('post.value');
            $data['tx_back_time'] = date('Y-m-d H:i:s');
            $info = model('MoneyDetailed')
                ->where('id', input('post.id'))
                ->find();
            $rst = model('User')->where('id', $info['us_id'])->setInc('us_all_get', $info['money']);
            $data['over_pay_money'] = model('User')->where('id', $us_id)->value('us_all_get');

            $up = model('MoneyDetailed')->save($data, ['id' => input('post.id')]);
            if ($rst && $up) {
                return ['code' => '1', 'msg' => '驳回成功'];
            } else {
                return ['code' => '0', 'msg' => '驳回失败'];
            }
        }
    }


    // 运营中心申请通过
    public function changezx()
    {
        if (is_post()) {
            $data[input('post.key')] = input('post.value');
            $data['tx_add_time'] = date('Y-m-d H:i:s');
            $rel = model('tixian')->save($data, ['id' => input('post.id')]);
            if ($rel) {
                return ['code' => '1', 'msg' => '审批成功'];
            } else {
                return ['code' => '0', 'msg' => '审批失败'];
            }
        }
    }

    //  运营中心申请驳回
    public function changezx2()
    {
        if (is_post()) {
            $data[input('post.key')] = input('post.value');
            $data['tx_back_time'] = date('Y-m-d H:i:s');

//            $data['y_pay_money'] = model('user')->where('id', $us_id)->value('us_all_get');
//            $data[input('post.key')] = input('post.value');
//            $data['tx_back_time'] = date('Y-m-d H:i:s');
//            $info = model('MoneyDetailed')
//                ->where('id', input('post.id'))
//                ->find();
//            $rst = model('User')->where('id', $info['us_id'])->setInc('us_all_get', $info['money']);
//            $data['over_pay_money'] = model('User')->where('id', $us_id)->value('us_all_get');

            $up = model('tixian')->save($data, ['id' => input('post.id')]);
            if ($up) {
                return ['code' => '1', 'msg' => '驳回成功'];
            } else {
                return ['code' => '0', 'msg' => '驳回失败'];
            }
        }
    }


    //申请运营中心
    public function operate()
    {

//        halt('1111111111');
        if (input('get.keywords')) {
            $us_id = model("User")->where('us_nickname|us_tel', input('get.keywords'))->value('id');
            if (!$us_id) {
                $us_id = 0;
            }
            $this->map[] = ['us_id', '=', $us_id];
        }
        if (input('get.start') && input('get.end') == "") {
            $this->map[] = ['tx_add_time', '>=', input('get.start')];
        }
        if (input('get.start') == "" && input('get.end')) {
            $this->map[] = ['tx_add_time', '=<', input('get.end')];
        }
        if (input('get.start') && input('get.end')) {
            $this->map[] = ['tx_add_time', 'between', array(input('get.start'), input('get.end'))];
        }
        if (input('get.wa_type') != "") {
            $this->map[] = ['wa_type', '=', input('get.wa_type')];
        }

        //$field = 'a.*,b.us_nickname';
        $list = model('Tixian')->selects($this->map, $this->order, $this->size);

        foreach ($list as $k => $v) {
            $list[$k]['tx_account'] = model('User')->where('id', $v['us_id'])->value('us_tel');
        }
//        halt($list);
        $this->assign(array(
            'list' => $list,
        ));
        return $this->fetch();

    }


    public function getAllChildrenId($id)
    {
        $ids = $this->getChildrenId($id) ?? [];
        $noHandleIds = $ids;
        while (!empty($noHandleIds)) {
            $id = array_shift($noHandleIds);
            $childrenIds = $this->getChildrenId($id);
            foreach ($childrenIds as $v) {
                array_push($ids, $v);
                array_push($noHandleIds, $v);
            }
        }
        return $ids;
    }

    public function getChildrenId($id)
    {
        return db('user')
            ->where('us_pid', $id)
            ->column("id");
    }


    /**
     * 数组 转 对象
     *
     * @param array $arr 数组
     * @return object
     */

    public function array_to_object($arr)
    {
        if (gettype($arr) != 'array') {
            return;
        }
        foreach ($arr as $k => $v) {
            if (gettype($v) == 'array' || getType($v) == 'object') {
                $arr[$k] = (object)array_to_object($v);
            }
        }

        return (object)$arr;
    }


}